﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NFine.Data;
using NFine.Domain.Entity.ProductManage;
using NFine.Domain.IRepository.ProductManage;

using System.Data.Entity;
using System.Reflection;

namespace NFine.Repository.ProductManage
{
   public  class ProductRepository : RepositoryBase<ProductEntity>, IProductRepository
   {
       
       public int SubmitForm(ProductEntity pEntity, string keyValue,  string newPath)
       {
           try
           {
               //开启事务
               using (var db = new RepositoryBase().BeginTrans())
               {
                   if (!string.IsNullOrEmpty(keyValue))
                   {
                       //删除之前的商品分类信息和商品属性信息及商品图片信息
                     //  db.Delete<ProductCategoryEntity>(a => a.F_ProductId == keyValue);
                      // db.Delete<ProductAttributeEntity>(a => keyValue.Contains(a.F_ProductId));
                       db.Delete<ProductImageEntity>(a => a.F_ProductId == keyValue);
                       //修改规格-(根据产品ID)先删除原有规格再重新添加
                      // db.Delete<ProductSkuItemsEntity>(a => keyValue.Contains(a.F_ProductId));
                      // db.Delete<ProductSkuEntity>(a => keyValue.Contains(a.F_ProductId));
                      // db.Delete<ProductSkuRelationEntity>(a => keyValue.Contains(a.F_ProductId));
                       
                       pEntity.Modify(keyValue);
                       db.Update1(pEntity);
                   }
                   else
                   {
                       pEntity.Create();
                       db.Insert(pEntity);
                   }
                  /* //保存商品所属的分类
                   ProductCategoryEntity cateEntity = new ProductCategoryEntity();
                   cateEntity.F_CategoryPath = pEntity.NotMapped_cate;

                   if (!string.IsNullOrEmpty(pEntity.NotMapped_cate) &&pEntity.NotMapped_cate!= "&nbsp;")
                   {
                       foreach (string s in pEntity.NotMapped_cate.Split(','))
                       {
                           cateEntity.F_Categorys += s.Substring(s.Length - 36) + ",";
                       }
                       cateEntity.F_Categorys = cateEntity.F_Categorys.TrimEnd(',');
                   }
                   else
                   {
                       cateEntity.F_Categorys = "";
                   }
                   cateEntity.F_ProductId = pEntity.F_Id;
                   cateEntity.Create();
                   db.Insert<ProductCategoryEntity>(cateEntity);*/
                 

                   //保存商品其他图片路径
                   if (!string.IsNullOrEmpty(pEntity.NotMapped_Images))
                   {
                       List<ProductImageEntity> imageList = new List<ProductImageEntity>();
                       foreach (var s in pEntity.NotMapped_Images.Split(','))
                       {
                           if (!string.IsNullOrEmpty(s))
                           {
                               ProductImageEntity i = new ProductImageEntity();
                               i.F_ProductId = pEntity.F_Id;
                               if (!s.StartsWith("/UploadFiles/Product"))//重新上传过图片
                               {
                                   i.F_ImageUrl = newPath + s;
                               }
                               else
                               {
                                   i.F_ImageUrl = s;
                               }
                               i.Create();
                               imageList.Add(i);
                           }
                       }
                       db.Insert<ProductImageEntity>(imageList);
                   }
                 
                   return db.Commit();
               }
           }
           catch
           {
               return 0;
           }
       }
       /// <summary>
       /// 删除商品
       /// </summary>
       /// <param name="keyValues">商品id ，分割</param>
       public int DeleteProducts(string keyValues)
       {
           try
           {
                 //开启事务
               using (var db = new RepositoryBase().BeginTrans())
               {
                   //删除商品属性规格信息Product_ProductAttribute
                 //  db.Delete<ProductAttributeEntity>(a => keyValues.Contains(a.F_ProductId));
                   //删除商品类别关联表Product_ProductCategory
                 //  db.Delete<ProductCategoryEntity>(a =>  keyValues.Contains(a.F_ProductId));
                   //删除商品图片表Product_ProductImage
                   db.Delete<ProductImageEntity>(a => keyValues.Contains(a.F_ProductId));

                   //删除商品SKU表Product_Sku
                 //  db.Delete<ProductSkuEntity>(a => keyValues.Contains(a.F_ProductId));
                   //删除SKU项表Product_SkuItems
                  // db.Delete<ProductSkuItemsEntity>(a => keyValues.Contains(a.F_ProductId));
                   //删除产品SKU项关系表Product_SkuRelation
                  // db.Delete<ProductSkuRelationEntity>(a => keyValues.Contains(a.F_ProductId));
                   //删除商品
                   db.Delete<ProductEntity>(t => keyValues.Contains(t.F_Id));
                   
                   return db.Commit();
               }
           }
           catch
           {
               return 0;
           }
       }

       public int inStock(string ids, int flag)
       {
           return dbcontext.Database.ExecuteSqlCommand(string.Format("update Product_Products set F_EnabledMark={0} where F_Id in ({1})", flag, ids));
       }
       public int auditProducts(string ids)
       {
           return dbcontext.Database.ExecuteSqlCommand(string.Format("update Product_Products set F_SaleStatus=1 where F_Id in ({0})",  ids));
       }
   }
}
